'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { STRING, INTEGER, DATE, BIGINT, BOOLEAN, TEXT, TINYINT } = app.Sequelize;
    return app.model.define(
        'ProductCategory',
        {
            id: {
                type: BIGINT.UNSIGNED,
                primaryKey: true,
                autoIncrement: true,
                comment: '主键ID'
            },
            name: {
                type: STRING(100),
                allowNull: false,
                unique: true,
                comment: '分类名称'
            },
            description: {
                type: TEXT,
                allowNull: true,
                comment: '分类描述'
            },
            sort_order: {
                type: INTEGER,
                allowNull: false,
                defaultValue: 0,
                comment: '排序值'
            },
            icon_url: {
                type: STRING(255),
                allowNull: true,
                comment: '图标图片地址'
            },
            status: {
                type: TINYINT,
                allowNull: false,
                defaultValue: 1,
                comment: '状态(0:禁用 1:启用)'
            },
            creator: {
                type: STRING(64),
                allowNull: false,
                defaultValue: '',
                comment: '创建者'
            },
            create_time: {
                type: DATE,
                allowNull: false,
                defaultValue: DATE.NOW,
                comment: '创建时间',
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updater: {
                type: STRING(64),
                allowNull: false,
                defaultValue: '',
                comment: '更新者'
            },
            update_time: {
                type: DATE,
                allowNull: false,
                defaultValue: DATE.NOW,
                comment: '更新时间',
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            deleted: {
                type: BOOLEAN,
                allowNull: false,
                defaultValue: false,
                comment: '是否删除'
            }
        },
        {
            tableName: 'product_category',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time',
            indexes: [
                {
                    name: 'uniq_name',
                    unique: true,
                    fields: ['name']
                },
                {
                    name: 'idx_sort_order',
                    fields: ['sort_order']
                },
                {
                    name: 'idx_deleted',
                    fields: ['deleted']
                }
            ]
        }
    );
};
